package cn.edu.sdjzu.xg11.dao;

import java.util.Collection;
import java.util.Date;
import java.util.TreeSet;

import org.springframework.stereotype.Repository;

import cn.edu.sdjzu.xg11.domain.UserInfo;
@Repository
public final class UserInfoDao {
	private static Collection<UserInfo> userInfos;
	static{
		TeacherDao teacherDao = TeacherDao.getInstance();
		userInfos = new TreeSet<UserInfo>();
		UserInfo user = new UserInfo(1,"stg","stg",new Date(),teacherDao.getTeacher(1));
		userInfos.add(user);
		userInfos.add(new UserInfo(2,"lx","lx",new Date(),teacherDao.getTeacher(2)));
		userInfos.add(new UserInfo(3,"wx","wx",new Date(),teacherDao.getTeacher(3)));
	}
	public Collection<UserInfo> getUserInfos(){
		return UserInfoDao.userInfos;
	}
	
	public UserInfo getUserInfo(Integer id){
		UserInfo desiredUserInfo = null;
		for (UserInfo userInfo : userInfos) {
			if(id.equals(userInfo.getId())){
				desiredUserInfo =  userInfo; 
				break;
			}
		}
		return desiredUserInfo;
	}
	
	public boolean updateUserInfo(UserInfo userInfo){
		userInfos.remove(userInfo);
		return userInfos.add(userInfo);		
	}
	
	public boolean addUserInfo(UserInfo userInfo){
		return userInfos.add(userInfo);		
	}

	public boolean deleteUserInfo(Integer id){
		UserInfo userInfo = this.getUserInfo(id);
		return this.deleteUserInfo(userInfo);
	}
	
	public boolean deleteUserInfo(UserInfo userInfo){
		return userInfos.remove(userInfo);
	}
	
	
	public static void main(String[] args){
		UserInfoDao dao = new UserInfoDao();
		Collection<UserInfo> userInfos = dao.getUserInfos();
		display(userInfos);
		;
	}

	private static void display(Collection<UserInfo> userInfos) {
		for (UserInfo userInfo : userInfos) {
			System.out.println(userInfo);
		}
	}
	
	
}
